﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using DcsGauges.Gui.Gauges.Interfaces;
using DcsGauges.Gui.Rendering.Interfaces;
using DcsGauges.Gui.Rendering;
using DcsGauges.DataStructures;
using Microsoft.Xna.Framework;
using System.Diagnostics;
using DcsGauges.Shared.DataStructures;

namespace DcsGauges.Gui.Gauges
{
    class CircularGauge : GuiRenderableBase, IValueGauge 
    {
        protected Texture2D                     backgroundTexture                           = null;
        protected Texture2D                     arrowTexture                                = null;
        protected IGaugeValueConverter<float>  valueConverter                               = null;
        protected DcsProperty                  dataProperty                                 = null;

        public CircularGauge(GuiRenderableBase parent, URect area, DcsProperty property, IGaugeValueConverter<float> valueConverter, Texture2D backgroundTexture, Texture2D arrowTexture) 
            : base(parent, area)
        {
            this.backgroundTexture          = backgroundTexture;
            this.arrowTexture               = arrowTexture;
            this.valueConverter             = valueConverter;
            this.dataProperty               = property;
        }

        public float Value
        {
            get;
            set;
        }

        public override void Render(SpriteBatch spriteBatch, GraphicsDevice device, Microsoft.Xna.Framework.Rectangle parentRect, float globalZIndex)
        {
            if(this.backgroundTexture != null)
            {
                Rectangle rect = this.CalcViewportRect(parentRect, this.backgroundTexture.Bounds);
                spriteBatch.Draw(this.backgroundTexture, rect, null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, globalZIndex);
            }
            if(this.arrowTexture != null)
            {
                float rotation  = this.valueConverter.Convert(this.Value);
                Rectangle rect  = this.CalcViewportRect(parentRect);
                Vector2 origin  = new Vector2(this.arrowTexture.Width / 2, this.arrowTexture.Height / 2);
                rect.X          += rect.Width / 2;
                rect.Y          += rect.Height / 2;
                spriteBatch.Draw(this.arrowTexture, rect, null, Color.White, rotation,
                    origin, SpriteEffects.None, this.NextFrontIndex(globalZIndex));
            }
        }

        public DcsProperty DataProperty
        {
            get { return this.dataProperty; }
        }
    }
}
